import QueryChooser from 'components/Markdown/QueryChooser'
import Collapse from 'components/Markdown/Collapse'
import Code from 'components/Markdown/Code'

export const meta = {
  title: 'Setting up Prisma',
  position: 1,
  gettingStartedOrder: 3,
  gettingStartedTitle: 'Demo database',
  nextText: 'Great work! 👏  Move on to learn how you can change your datamodel and regenerate your Prisma client.',
  technology: 'node',
  technologyOrder: 2,
  articleGroup: 'Setting up Prisma',
}

## Goals

On this page, you will learn how to:

- Install the Prisma CLI
- Setup Prisma with a sandboxed demo database
- Read and write data using the Prisma client

## Install the Prisma CLI

The [Prisma CLI](alx4) is used for various Prisma workflows. You can install it using [Homebrew](https://brew.sh/) or [NPM](https://www.npmjs.com/):

<Code languages={["Homebrew", "NPM"]}>

```bash copy
brew tap prisma/prisma
brew install prisma
```

```bash copy
npm install -g prisma
```

</Code>

## Setup Prisma

To bootstrap the configuration files for your Prisma setup, create a new directory and initalize it using the `prisma init` command:

```bash copy
mkdir hello-world
cd hello-world
prisma init
```

After running `prisma init`, the Prisma CLI prompts you to select _how_ you want to deploy Prisma:

1. Select **Demo server** from the list.
1. When your browser opens, register with [Prisma Cloud](https://www.prisma.io/cloud/). This is needed because that's where the Demo server is hosted.
1. Go back to your terminal.
1. Confirm the suggested values for the following questions with **Return**:
    1. The **region** where Prisma service should be hosted
    1. The **name** for Prisma service
    1. The **stage** for Prisma service
1. Select **Prisma JavaScript Client** to generate Prisma client for Node.js.

<Collapse title="Learn about the generated files">

- `prisma.yml`: The root configuration file for your Prisma setup.
- `datamodel.prisma`: Specifies the datamodel for your application that will be mapped to the database (it basically defines your database schema).

</Collapse>

## Deploy Prisma

The interactive wizard created the _minimal_ Prisma configuration based on a hosted demo database: `prisma.yml` and `datamodel.prisma`. Prisma now needs to be _deployed_ so you can [use the Prisma API](nms2):

```bash copy
prisma deploy
```

Congratulations, you have successfully deployed Prisma. You can now start using the Prisma client to talk to your database from code.

## Prepare Node application

Run the following command to create an empty Node script:

```bash copy
touch index.js
```

Next, initialize an empty NPM project in the current directory and install the required dependencies:

```bash copy
npm init -y
npm install --save prisma-client-lib graphql@0.13
```

## Read and write data using the Prisma client

Add the following code in `index.js` and save it afterwards:

```js copy
const { prisma } = require('./generated/prisma')

// A `main` function so that we can use async/await
async function main() {

  // Create a new user called `Alice`
  const newUser = await prisma.createUser({ name: 'Alice' })
  console.log(`Created new user: ${newUser.name} (ID: ${newUser.id})`)

  // Read all users from the database and print them to the console
  const allUsers = await prisma.users()
  console.log(allUsers)
}

main().catch(e => console.error(e))
```

Execute the script with the following command:

```bash copy
node index.js
```

Whenever you run the script with that command, a new user record is created in the demo database (because of the call to `createUser`).

Feel free to play around with the Prisma client API and try out some of the following operations by adding the following code snippets to the file (at the end of the `main` function) and re-executing the script:

<QueryChooser titles={["Fetch single user", "Filter user list", "Update a user's name", "Delete user"]}>

```js lineNumbers,copy
const user = await prisma
  .user({ id: '__USER_ID__' })
```

```js lineNumbers,copy
const usersCalledAlice = await prisma
  .users({
    where: {
      name: 'Alice'
    }
  })
```

```js lineNumbers,copy
 const updatedUser = await prisma
  .updateUser({
    where: { id: '__USER_ID__' },
    data: { name: 'Bob' }
  })
```

```js lineNumbers,copy
 const deletedUser = await prisma
  .deleteUser({ id: '__USER_ID__' })
```

</QueryChooser>

> In some snippets, you need to replace the `__USER__ID__` placeholder with the ID of an actual user.
